package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference //远程查找
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }

    //按照用户Id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            User user =userService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }


    //根据用户id查询角色id
    @RequestMapping("/findRoleIdByUserId")
    public Result findRoleIdByUserId(Integer id){


        try{
            List<Integer> roles=userService.findRoleIdByUserId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,roles);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user,Integer [] roles){
        //密码进行加盐处理
        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);
        try{
            userService.add(user,roles);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    //编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer [] roles){
        try{
            userService.edit(user,roles);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESSS);
    }

    //删除
    @RequestMapping("delete")
    public Result delete(Integer id){
        //得到id 根据id进行删除
        try{
            userService.delete(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

}
